/*
 * @lc app=leetcode.cn id=706 lang=cpp
 *
 * [706] 设计哈希映射
 */
#include "include.h"
// @lc code=start
class MyHashMap {
public:
    MyHashMap() {
        hashTable_.resize(modNum, std::list<std::pair<int, int>>());
    }
    
    void put(int key, int value) {
        int index = key % modNum;
        int mod = key / modNum;
        if (hashTable_[index].empty()){
            // not found
            hashTable_[index].emplace_back(mod, value);
            return;
        }
        
        auto& currentList = hashTable_[index];
        for (auto& pair : currentList){
            if (pair.first == mod){
                // found
                pair.second = value;
                return;
            }
        }
        currentList.emplace_back(mod, value);
        return;
    }
    
    int get(int key) {
        int index = key % modNum;
        if (hashTable_[index].empty()){return -1;}

        int mod = key / modNum;
        auto& currentList = hashTable_[index];
        for (auto& pair : currentList){
            if (pair.first == mod){
                // found
                return pair.second;
            }
        }
        return -1;
    }
    
    void remove(int key) {
        int index = key % modNum;
        if (hashTable_[index].empty()){return;}

        int mod = key / modNum;
        auto& currentList = hashTable_[index];
        for (auto pairIter = currentList.begin();
                  pairIter != currentList.end();){
            if (pairIter->first == mod){
                // found
                pairIter = currentList.erase(pairIter);
                return;
            }
            pairIter++;
        }
        return;
    }

private:
    const int modNum = 199;
    // hashArray => list => (mod, value)
    std::vector<std::list<std::pair<int, int>>> hashTable_;

    int find(int key){
        int index = key % modNum;
        if (hashTable_[index].empty()){return -1;}

        int mod = key / modNum;
        auto& currentList = hashTable_[index];
        for (auto& pair : currentList){
            if (pair.first == mod){
                // found
                return pair.second;
            }
        }
        return -1;
    }
};

/**
 * Your MyHashMap object will be instantiated and called as such:
 * MyHashMap* obj = new MyHashMap();
 * obj->put(key,value);
 * int param_2 = obj->get(key);
 * obj->remove(key);
 */
// @lc code=end

